<?php 
namespace Api\Model;
use Think\Model;
/*
	模块关键字添加
*/
class RuleModel extends Model {


	public function Post(){
		
		$modulename = strtolower(MODULE_NAME);
		
		/*当是默认关注的图文回复是新加的时候*/
		$postmodule = I('postmodule');
		$news_title_new =  I('news-title-new');
		if($postmodule == 'news' &&  $news_title_new != ''){
			$rid = '';
		}else{
			$rid = intval(I('rule_id'));
		}
		$title = I('title');
		if (empty($title)) {
			$message=array(
				'info'=>'抱歉，规则名称为必填项，请选回修改！',
				'status'=>0,
				'rule_id'=>0,
			);
			return $message;
		}
		$status = I('status');
		
		$rule = array(
			'cid' => '',
			'name' => $title,
			'module' => $modulename,
			'status' => intval($status) ? intval($status) : 1 ,
		);
		$istop = I('istop');
		if (!empty($istop)) {
			$rule['displayorder'] = 255;
		} else {
			$displayorder = I('displayorder');
			$rule['displayorder'] = intval($displayorder) > 254 ? 254 : intval($displayorder);
		}
		
		
		//调用模块处理

		if (!empty($rid)) {
			$isexists = M('Rule')->where('id = %d',$rid)->field('id,module')->find();
			if (empty($isexists)) {
				$message=array(
					'info'=>'抱歉，要修改的规则不存在或是已经被删除！',
					'status'=>0,
					'rule_id'=>0,
				);
				return $message;
			}
			$rule['module'] = $isexists['module'];
			$result = M('Rule')->where('id = %d',$rid)->save($rule);
		} else {
			$rid = M('Rule')->add($rule);
		}

		if (!empty($rid)) {
			//更新，添加，删除关键字
			M('RuleKeyword')->where('rule_id = %d ',$rid)->delete();
			$rows = array();
			$rowtpl = array(
				'rule_id' => $rid,
				'module' => $rule['module'],
				'status' => $rule['status'],
				'displayorder' => $rule['displayorder'],
			);
			$keyword_name = I('keyword-name');
			if (!empty($keyword_name)) {
				foreach ($keyword_name as $id => $row) {
					if (empty($row) && strlen($row) == 0 && intval($keyword_name[$id]) != 4) {
						continue;
					}
					$rowtpl['content'] = $row;
					$keyword_type = I('keyword-type');
					$rowtpl['type'] = intval($keyword_type[$id]);
					if($rowtpl['type'] == 4) {
						$rowtpl['content'] = '';
					}
					$rows[md5($rowtpl['type'] . $rowtpl['content'])] = $rowtpl;
				}
			}
			$keyword_name_new = I('keyword-name-new');
			if (!empty($keyword_name_new)) {
				foreach ($keyword_name_new as $id => $row) {
					if (empty($row) && strlen($row) == 0 && intval($keyword_name_new[$id]) != 4) {
						continue;
					}
					$rowtpl['content'] = $row;
					$keyword_type_new = I('keyword-type-new');
					$rowtpl['type'] = intval($keyword_type_new[$id]);
					if($rowtpl['type'] == 4) {
						$rowtpl['content'] = '';
					}
					$rows[md5($rowtpl['type'] . $rowtpl['content'])] = $rowtpl;
				}
			}

			$keywords = I('keywords');
			if(!empty($keywords)) {
				$kwds = explode(',', trim($keywords));
				foreach($kwds as $kwd) {
					$kwd = trim($kwd);
					if(empty($kwd)) {
						continue;
					}
					$rowtpl['content'] = $kwd;
					$rowtpl['type'] = 1; 
					$rows[md5($rowtpl['type'] . $rowtpl['content'])] = $rowtpl;
				}
			}

			foreach($rows as $krow) {
				$result = M('RuleKeyword')->add($krow);
			}

			$message=array(
				'info'=>'规则操作成功！',
				'status'=>1,
				'rule_id'=>$rid,
			);
			return $message;
		} else {
			$message=array(
				'info'=>'规则操作失败！',
				'status'=>0,
				'rule_id'=>0,
			);
			return $message;
		}

		
		
			
		
		
	}

	/**
	*
	* 返回规则标题关键字
	*/
	public function DisplayRule($rule_id = 0) {
		$weid = session('weid');
		$uid = session('uid');
		
		
		$types = array(
			1 => array(
					'name' => '完全等于关键字',
					'description' => '用户进行微信交谈时，对话内容完全等于上述关键字才会执行这条规则。',
			),    
			2 => array(
				'name' => '包含关键字',
				'description' => '用户进行交谈时，对话中包含上述关键字就执行这条规则。',
			),
			3 => array(
				'name' => '自定义匹配',
				'description' => "用户进行交谈时，对话内容符合述关键字中定义的模式才会执行这条规则。<br/><br/><strong>示例: </strong><br/><b>^微购王</b>匹配以“微购王”开头的语句<br /><b>微购王$</b>匹配以“微购王”结尾的语句<br /><b>^微购王$</b>匹配等同“微购王”的语句<br /><b>微购王</b>匹配包含“微购王”的语句<br /><b>[0-9\.\-]</b>匹配所有的数字，句号和减号<br /><b>^[a-zA-Z_]$</b>所有的字母和下划线<br /><b>^[[:alpha:]]{3}$</b>所有的3个字母的单词<br /><b>^a{4}$</b>aaaa<br /><b>^a{2,4}$</b>aa，aaa或aaaa<br /><b>^a{2,}$</b>匹配多于两个a的字符串",
			),
			#4 => array(
			#	'name' => '直接接管',
			#	'description' => "如果没有比这条回复优先级更高的回复被触发，那么直接使用这条回复。<br/>",
			#)
		);
		
		$module = strtolower(MODULE_NAME);
		$defaultmodule = !empty($module) ? $module : 'basic';
		

		$typeslabel = "'".implode("','", $types)."'";

		$rule_id = $rule_id ? $rule_id : intval(I('id'));
		if (!empty($rule_id)) {
			$rule['rule'] = M('Rule')->where('id = %d',$rule_id)->find();
			$rule['keyword'] = M('RuleKeyword')->where('rule_id = %d',$rule_id)->select();
			
			if (empty($rule['rule'])) {
				return array('info'=>'抱歉，您操作的规则不在存或是已经被删除！','status'=>0,'rule_id'=>0);
			}

			$module = $rule['rule']['module'];
			$rule['reply'] = $module;
			$rule['kwd-adv'] = false;
			$rule['keywords'] = array();
			foreach($rule['keyword'] as $kwd) {
				if($kwd['type'] != '1') {
					$rule['kwd-adv'] = true;
				} else {
					$rule['keywords'][] = $kwd['content'];
				}
			}
			$rule['keywords'] = implode(',', $rule['keywords']);
		}

		return array('info'=>$rule,'status'=>1,'rule_id'=>$rule_id);
	
	}


	public function DeleteRule(){
		$rid = intval(I('id'));

		$rule_id = M('Rule')->where(array('id'=>$rid))->getField('id');

		if (empty($rule_id)) {
			return array('info'=>'抱歉，要修改的规则不存在或是已经被删除！','status'=>0);
		}
		//删除回复，关键字及规则
		$ac = M('Rule')->where(array('id'=>$rule_id))->delete();
		if ($ac) {
			M('RuleKeyword')->where(array('rule_id' => $rid))->delete();
			//调用模块中的删除
			return array('info'=>'删除成功','status'=>1);
		}
	}

}
